﻿@page "/register"
@inject IUserAccountServices UserAccountServices
@inject IPopupService PopupService
@inject ILocalStorageService LocalStorage
@inject NavigationManager NavigationManager

<MCard Style="position: absolute; top: 50%;left: 50%;transform: translate(-50%, -50%);" Width="800" Height="500" Elevation="10">
    <MContainer Fluid Style="height:100%; padding:0px">
        <MRow Style="height:100%; border-radius:5px;position:relative" NoGutters>
            <MCol Style="width:50%;height:100%;position:relative;">
                <p style="margin-top:30px;display:flex;justify-content:center;font-family:'Microsoft JhengHei';font-size:x-large">长春市旅游信息服务平台</p>
                <MImage Src="imgs/logo.png" Style="position:absolute;left:21%;right:21%;top:120px;margin:auto;border-radius:50%" />
            </MCol>
            <MDivider Vertical Inset Style="position:absolute;left:50%;height:90%;top:5%;margin:0" />
            <MCol>
                <p style="margin-top:30px;display:flex;justify-content:center;font-size:x-large;font-weight:200">用户注册</p>
                <div style="margin-top:10%;position:relative;">
                    <MTextField TValue="string" Style="width:80%;position:absolute;left:0;right:0;margin:auto;"
                                Solo Label="用户名" Hint="由字母、数字和下划线组成" PrependInnerIcon="mdi-account-circle-outline"
                                Color="#4DB6AC" @bind-Value="Username" />
                    <MTextField TValue="string" Style="width:80%;position:absolute;left:0;right:0;top:80px;margin:auto;"
                                Solo Label="密码"
                                Hint="最少8位，最多20位" PrependInnerIcon="mdi-key"
                                AppendIcon="@(ShowPassword ? "mdi-eye" : "mdi-eye-off")"
                                Type="@(ShowPassword ? "text" : "password")" Color="#4DB6AC"
                                Counter="true"
                                OnAppendClick="() => ShowPassword = !ShowPassword" @bind-Value="Password1" />
                    <MTextField TValue="string" Style="width:80%;position:absolute;left:0;right:0;top:160px;margin:auto;"
                                Solo Label="确认密码"
                                Hint="最少8位，最多20位" PrependInnerIcon="mdi-key"
                                AppendIcon="@(ShowPassword ? "mdi-eye" : "mdi-eye-off")"
                                Type="@(ShowPassword ? "text" : "password")" Color="#4DB6AC"
                                Counter="true"
                                OnAppendClick="() => ShowPassword = !ShowPassword" @bind-Value="Password2" />
                    <MButton Style="position:absolute;left:40%;right:40%;top:280px;margin:auto;color:white" Color="#4DB6AC" OnClick="RegisterUser">
                        注册
                    </MButton>
                    <p style="color:red; font-size:x-small;position:absolute;left:45px;right:0;top:240px;margin:auto;">@ErrorMessage</p>
                </div>
                <p style="position:absolute;bottom:20px;right:20px;font-size:x-small">已有账号？<a href="/">立即登录</a></p>
            </MCol>
        </MRow>
    </MContainer>
</MCard>

@code {
    public string ErrorMessage { get; set; } = " ";

    public string Password1 { get; set; } = string.Empty;

    public string Password2 { get; set; } = string.Empty;

    public string Username { get; set; } = string.Empty;

    public bool ShowPassword { get; set; } = false;

    private async Task RegisterUser()
    {
        ErrorMessage = string.Empty;
        if (string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password1) || string.IsNullOrWhiteSpace(Password1))
        {
            ErrorMessage = "请完整填写注册信息";
            return;
        }

        if(Password1!=Password2)
        {
            ErrorMessage = "两次输入的密码不一致";
            return;
        }

        var regResult = await UserAccountServices.RegisterAsync(new User(Username, Password1));
        StateHasChanged();

        if(!regResult.Succeed)
        {
            ErrorMessage = regResult.Message;
            return;
        }

        await PopupService.AlertAsync($"注册成功！账号为{regResult.NewUser?.Account}。即将跳转至主页", AlertTypes.Success);
        await LocalStorage.SetItemAsync("user", regResult.NewUser);
        NavigationManager.NavigateTo("/index");
        return;
    }
}
